package com.next.aap.server.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.next.aap.client.AllItem;
import com.next.aap.server.cache.CandidateCache;
import com.next.aap.server.cache.DelhiElectionCache;
import com.next.aap.server.cache.DonationCache;
import com.next.aap.server.cache.FacebookCache;
import com.next.aap.server.cache.LocationCache;
import com.next.aap.server.cache.ManifestoCache;
import com.next.aap.server.services.AppService;
import com.next.aap.server.tasks.TaskManager;

public class BaseController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	protected FacebookCache facebookCache;
	@Autowired
	protected DelhiElectionCache delhiElectionCache;
	@Autowired
	protected DonationCache donationCache;
	@Autowired
	protected CandidateCache candidateCache;
	@Autowired
	protected AppService appService;
	@Autowired
	protected TaskManager taskManager;
	@Autowired
	protected LocationCache locationCache;
	@Autowired
	protected ManifestoCache manifestoCache;


	protected String convertToJson(AllItem allItem){
		Gson gson = new Gson();
		String response = gson.toJson(allItem);
		writeToFile(response);
		return response;
	}
	protected String getFileName(){
		return "allitem.json";
	}
	protected void writeToFile(String jsonData){
		String fileName = getFileName();
		System.out.println("fileName = "+fileName);
		logger.info("fileName = {}",fileName);
		BufferedWriter writer = null;
		try
		{
			logger.info("Writing Data = {}",jsonData);
			writer = new BufferedWriter( new FileWriter( fileName));
			writer.write( jsonData);
			logger.info("Writing Data finished ");
		}
		catch ( IOException e)
		{
			logger.error("Error in writing File",e);
		}
		finally
		{
			try
			{
				if ( writer != null)
					writer.close( );
			}
			catch ( IOException e)
			{
				logger.error("Error in closing File",e);
			}
	     }
		try{
			File file = new File(fileName);
			logger.info("getAbsolutePath = {} ",file.getAbsolutePath());
			logger.info("getCanonicalPath = {} ",file.getCanonicalPath());
			logger.info("Path = {} ",file.getPath());
			logger.info("parent = {} ",file.getParent());
			
		}catch(Exception ex){
			
		}
	}
	protected String readFromFile(){
		String fileName = getFileName();
		System.out.println("fileName = "+fileName);
		logger.info("fileName = {}",fileName);
		BufferedReader reader = null;
		String jsonData = null;
		try
		{
			reader = new BufferedReader( new FileReader( fileName));
			jsonData = reader.readLine();

		}
		catch ( IOException e)
		{
			logger.error("Error in reading File",e);
		}
		finally
		{
			try
			{
				if ( reader != null)
					reader.close( );
			}
			catch ( IOException e)
			{
				logger.error("Error in closing(Reading) File",e);
			}
	     }
		return jsonData;
	}
}
